<template>
    <div class="listDiv pd10">
        <div class="searchDiv mb20">
            <div class="flex1 flex-row-center">
                <el-icon @click="proxy.$router.go(-1)" class="backicon"><Back /></el-icon>
                <span class="title ml10">{{ proxy.$route.query.name||'' }}</span>
            </div>
            <el-button type="primary" @click="add">上传知识</el-button>
        </div>
        <div class="w100 flex-row wrap overflow-y">
            <div v-for="(v,i) in obj.ary" :key="i" class="filecard flex-row al-flex-start">
                <el-image :src="`/imgs/${v.filename.split(/\.(?=[^\.]+$)/)[1]}.png`" style="width: 36px;" fit="contain" class="mr10"></el-image>
                <div class="flex1 flex-column hidden">
                    <div class="flex-row-center mb10">
                        <el-tooltip
                            effect="dark"
                            :content="v.filename"
                            placement="top-start"
                        >
                            <div class="name flex1 lineOne mr20">{{ v.filename }}</div>
                        </el-tooltip>
                        <el-dropdown :ref="`dropdown${v.file_id}`" trigger="contextmenu">
                            <el-icon @click="showtab(v)"><More /></el-icon>
                            <template #dropdown>
                                <el-dropdown-menu>
                                    <el-dropdown-item @click="preview(v)">下载</el-dropdown-item>
                                    <el-dropdown-item @click="del(v)"><span style="color: red;">删除</span></el-dropdown-item>
                                </el-dropdown-menu>
                            </template>
                        </el-dropdown>
                    </div>
                    <div class="flex-row-center mb5">
                        <span class="desc">上传时间：{{ v.intime }}</span>
                        <!-- <span class="desc">知识类型：文章知识</span> -->
                    </div>
                    <!-- <div class="desc flex-row-center"><span class="lineOne" style="width: 150px;">文档ID:{{ v.id }}</span><el-icon class="pointer" @click="proxy.$util.copyText(v.id)"><DocumentCopy /></el-icon></div> -->
                </div>
            </div>
        </div>

        <div class="flex-center mt10">
            <el-pagination
                background
                size="small"
                @size-change="init()"
                @current-change="init()"
                v-model:current-page="obj.req.currentpage"
                v-model:page-size="obj.req.pagesize"
                :page-sizes="[10, 20, 50, 100]"
                layout="total, sizes, prev, pager, next, jumper"
                :total="obj.req.count">
            </el-pagination>
        </div>

        <el-drawer v-model="obj.drawer.show" :title="obj.drawer.title" size="50%" direction="rtl" :close-on-click-modal="false" destroy-on-close>
            <FileEdit v-model="obj.drawer" @init="init" @close="obj.drawer.show = false" />
        </el-drawer>
    </div>
</template>


<script setup>
    import { ref,reactive,getCurrentInstance,onMounted,computed } from 'vue'
    import { Back,DocumentDelete,DocumentCopy,More } from '@element-plus/icons-vue'
    import FileEdit from './fileEdit.vue'
    let { proxy } = getCurrentInstance()

    const dropdown1 = ref(null)
    let obj = reactive({
        drawer: {
            show: false,
            title: "",
            data: {}
        },
        req:{
            company_knowledge_id: proxy.$route.query.id,
            currentpage: 1,
            pagesize: 10,
            count: 0
        },
        ary:[],
    })

    onMounted(async()=>{
        init()
    })


    const showtab=(x)=>{
        if (!proxy.$refs[`dropdown${x.file_id}`][0]) return
        proxy.$refs[`dropdown${x.file_id}`][0].handleOpen()
    }

    let init = (t) => {
        obj.ary = []
        proxy.$ajax({
            url: '/manager/knowledge/file/list',
            data: obj.req,
            bfLoading: true
        }).then(d => {
            if(d.success){
                obj.ary = d.msg.list
                obj.req.count = d.msg.count
            }else{
                obj.ary = []
                obj.req.count = 0
            }
        }).catch(() => { })
    }

    let del = x => {
        proxy.$ajax({
            url: '/manager/knowledge/file/delete',
            data: { file_id: x.file_id},
            bfLoading: true
        }).then(da => {
            if(da.success){
                proxy.elMessageMixin(`成功`, 'success')
                init()
            }else{
                proxy.elMessageMixin(d.err, 'error')
            }
        }).catch(() => { })
    }

    let add = () => {
        obj.drawer.show = true
        obj.drawer.title = "上传知识"
        obj.drawer.data = {knowledge_id: obj.req.knowledge_id};
    }

    let preview = x => {
        window.open(`${proxy.$setting.globalUrl.fileurl}/knowledge/${x.filename}`)
    }
</script>

<style lang="scss" scoped>
.listDiv{min-width: 856px;}
.backicon{font-size: 22px;font-weight: bold;cursor: pointer;}
.title{font-size: 18px;font-weight: bold;}
.filecard{
    width: calc(25% - 9px);min-width: 200px;height: 120px;border-radius: 10px;border: solid 1px #dcdcdc;cursor: pointer;
    padding: 20px;box-sizing: border-box;margin-right: 12px; margin-bottom: 12px;
    .name{font-size: 16px;}
    .desc{position: relative;font-size: 12px;color: #999;padding-right: 6px;padding-left: 6px;}
    .desc::after{
        content: '';position: absolute;right: 0;top: 50%;margin-top: -6px;width: 1px;height: 12px;background: #888;
    }
    .desc:last-child::after{width: 0 !important;}
}
.filecard:nth-child(4n){margin-right: 0;}
.filecard:hover{background: #f0f0f0;}
</style>